package org.jz.gae.redbook.spb.client.slate.family;

import java.util.ArrayList;
import java.util.List;

import org.jz.gae.redbook.spb.client.toolkit.EntityViewGrid;
import org.jz.gae.redbook.spb.client.toolkit.EntityViewGrid.ColumnValueDisplayer;
import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class FamilyChoosePlate extends Composite
{
	  private final Button refreshButton = new Button("Обновить");
	  private final Button chooseButton = new Button("Выбрать");
	  private final Button cancelButton = new Button("Отмена");
	  private final FlexTable contentTable;
	  private EntityViewGrid<FamilyEntityView> familyListGrid;
	  private Slate slate;
	private FamilyChoosingListener familyChoosingListener;
	  
	  public interface FamilyChoosingListener
	  {
		  public void familyChoosen(FamilyEntityView _Family);
	  }
	  
	  public FamilyChoosePlate() 
	  {
		  SimplePanel contentTableDecorator = new SimplePanel();
	    initWidget(contentTableDecorator);
	    contentTableDecorator.setWidth("100%");

	    contentTable = new FlexTable();
	    contentTable.setWidth("100%");
	    //contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListContainer");
	    //contentTable.getCellFormatter().setWidth(0, 0, "100%");
	    contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);
	    
	    // Create the menu
	    //
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setBorderWidth(0);
	    hPanel.setSpacing(0);
	    hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
	    //contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListMenu");
	    contentTable.setWidget(0, 0, hPanel);
	    
	    // Create the contacts list
	    //
	    familyListGrid = new EntityViewGrid<FamilyEntityView>();
	    familyListGrid.setWidth("100%");
	    familyListGrid.addButton(refreshButton);
	    familyListGrid.addButton(chooseButton);
	    familyListGrid.addButton(cancelButton);
	    
	    familyListGrid.addGridColumn("Alias", "80px", new ColumnValueDisplayer<FamilyEntityView>(){
	    	@Override
	    	public String displayColumnValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyAlias();
	    	}
	    },
	    new TextColumn<FamilyEntityView>(){
	    	@Override
	    	public String getValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyAlias();
	    	}
	    });
    familyListGrid.addGridColumn("DT", "80px", new ColumnValueDisplayer<FamilyEntityView>(){
	    	@Override
	    	public String displayColumnValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyEnglerCode() + "";
	    	}
	    },
	    new TextColumn<FamilyEntityView>(){
	    	@Override
	    	public String getValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyEnglerCode() + "";
	    	}
	    });
    familyListGrid.addGridColumn("Family", "280px", new ColumnValueDisplayer<FamilyEntityView>(){
	    	@Override
	    	public String displayColumnValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyLatName();
	    	}
	    },
	    new TextColumn<FamilyEntityView>(){
	    	@Override
	    	public String getValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyLatName();
	    	}
	    });
    familyListGrid.addGridColumn("Семейство", null, new ColumnValueDisplayer<FamilyEntityView>(){
	    	@Override
	    	public String displayColumnValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyRusName();
	    	}
	    },
	    new TextColumn<FamilyEntityView>(){
	    	@Override
	    	public String getValue(FamilyEntityView _EntityView)
	    	{
	    		return _EntityView.getFamilyRusName();
	    	}
	    });

	    contentTable.setWidget(1, 0, familyListGrid);
	    
	    contentTableDecorator.add(contentTable);
	    
	    refreshButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	fetchFamilyDetails();
	        }
	      });
	    
	    chooseButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	chooseSelectedFamily();
	        }
	      });
	    cancelButton.addClickHandler(new ClickHandler() {   
	        public void onClick(ClickEvent event) 
	        {
	        	closePlate();
	        }
	      });
	  }
	  
	  public void chooseFamily(
			  Slate _Slate,
			  FamilyChoosingListener _FamilyChoosingListener)
	  {
		 slate = _Slate;
		 familyChoosingListener = _FamilyChoosingListener;
		 fetchFamilyDetails();  
		 slate.exposePlate(this);
	  }
	  
	  private void fetchFamilyDetails() 
	  {
		    slate.getContainerAgent().getRpcService().getFamilyDetails(new AsyncCallback<ArrayList<FamilyEntityView>>() {
		      public void onSuccess(ArrayList<FamilyEntityView> result) 
		      {
		    	  List<FamilyEntityView> familyDetails = result;
		          //sortFamilyDetails(familyDetails);
		          familyListGrid.setData(familyDetails);
		      }
		      
		      public void onFailure(Throwable caught) {
		        Window.alert("Error fetching family details");
		      }
		    });
		  }

      private void chooseSelectedFamily()
      {
      	FamilyEntityView family = familyListGrid.getSelectedEntityView();
      	if (family != null)
      	{
         	  slate.closeTopPlate();
         	  familyChoosingListener.familyChoosen(family);
      	}
      	else
      	{
              System.err.println("event releasing : no family chosen");  
      	}
      }
      
      private void closePlate()
      {
    	  slate.closeTopPlate();
      }
}
